import {
  voteDetails,
  voteSubmit
} from '../../apis/vote';
import {
  splicing,
  formatTime
} from '../../utils/util';
const app = getApp();

Page({
  data: {
    voteDetails: {},
    voteBtnActive: false,
    authorizationShow: false
  },

  onLoad(options) {
    this.setData({
      ...options
    });
    this.getVoteDetails(options.voteId);
  },

  onShareAppMessage() {
    return {
      title: this.data.voteDetails.voteName,
      path: `/pages/voteDetails/index?voteId=${this.data.voteId}`,
      imageUrl: this.data.voteDetails.image
    }
  },

  // 查看图片
  previewImage() {
    wx.previewImage({
      urls: [this.data.voteDetails.image],
      current: this.data.voteDetails.image
    });
  },

  // 获取授权信息
  bindgetuserinfo(e) {
    if (app.globalData.isAuthorization) {
      if (this.data.voteDetails.status == 1 && !this.data.voteDetails.voting) this.confirmVote();
    } else {
      this.setData({
        authorizationShow: true
      });
    };
  },

  // 授权回调
  callBack() {
    if (this.data.voteDetails.status == 1 && !this.data.voteDetails.voting) this.confirmVote();
  },

  // 获取投票详情
  getVoteDetails(id) {
    wx.showLoading({
      title: '加载中...'
    });
    voteDetails(id).then(res => {
      wx.hideLoading();
      if (res.data.code === 200) {
        let {
          data
        } = res.data;
        data.image = splicing(data.image);
        data.timeEnd = formatTime(data.timeEnd, 'data');
        data.optionList.forEach(v => {
          if (v.image) {
            v.image = splicing(v.image);
          };
        });
        this.setData({
          voteDetails: data
        });
      };
    });
  },

  // 选择投票项
  selectVoteItem(e) {
    let {
      index
    } = e.currentTarget.dataset;
    if (this.data.voteDetails.voteType == 1) {
      this.data.voteDetails.optionList.forEach((v, i) => {
        if (i == index) {
          v.selected = true;
        } else {
          v.selected = false;
        };
      });
      this.setData({
        'voteDetails.optionList': this.data.voteDetails.optionList
      });
    } else {
      this.data.voteDetails.optionList[index].selected = !this.data.voteDetails.optionList[index].selected;
      if (this.data.voteDetails.optionList.filter(v => v.selected).length > this.data.voteDetails.limitNum) {
        this.data.voteDetails.optionList[index].selected = !this.data.voteDetails.optionList[index].selected;
        wx.showToast({
          title: `最多选择${this.data.voteDetails.limitNum}项`,
          icon: 'none'
        });
        return
      };
      this.setData({
        'voteDetails.optionList': this.data.voteDetails.optionList
      });
    };

    if (this.data.voteDetails.optionList.some(v => v.selected)) {
      this.setData({
        voteBtnActive: true
      });
    } else {
      this.setData({
        voteBtnActive: false
      });
    };
  },

  // 确认投票
  confirmVote() {
    let arr = this.data.voteDetails.optionList.filter(v => v.selected).map(val => val.optionId);
    if (arr.length === 0) {
      wx.showToast({
        title: '请选择',
        icon: 'none'
      });
      return
    };

    wx.requestSubscribeMessage({
      tmplIds: ['iHc3918z8uQKnFRtZzzAWYFDpzoUJZTZGKsNtZUeefg'],
      complete: res => {
        wx.showLoading({
          title: '提交中...',
        });
        voteSubmit(this.data.voteId, arr).then(res => {
          wx.hideLoading();
          if (res.data.code === 200) {
            wx.showToast({
              title: '投票成功',
              icon: 'success',
              mask: true,
              duration: 1500
            });
            this.getVoteDetails(this.data.voteId);
          };
        });
      }
    });
  }
})